//
//  MOFoodOrderListDetailViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/10/18.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOFoodOrderListDetailViewController.h"
#import "MOFoodOrderListDetailViewCell.h"
#import "MOAppraiseViewController.h"
#import "MOFoodDetailView.h"

@interface MOFoodOrderListDetailViewController ()<UITableViewDelegate,UITableViewDataSource,YPSwipeTableViewCellDelegate>

@property(nonatomic,strong) UITableView *tableView;

@property(nonatomic,strong) NSMutableArray *buttonArray;

@property(nonatomic,strong) UIView *navView;

@property(nonatomic,strong) UIView *headerView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)NSMutableArray *labelArray;

@property(nonatomic,strong)NSDictionary *dataDic;

@end

@implementation MOFoodOrderListDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"订单详情";
    self.view.backgroundColor = KWhiteColor;
    
    [self.view addSubview:self.navView];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.tableHeaderView = self.headerView;
    
    [self getData];
}

-(void)getData
{
    if (self.orderId.length>0) {
        NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",GETORDERINFO,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
        NSDictionary *dic = @{
                              @"orderId":self.orderId
                              };
        [PPNetworkHelper POST:postUrl parameters:dic success:^(id responseObject) {
            NSLog(@"订单详情：%@",responseObject);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                
                NSArray *arr = responseObject[@"data"][@"details"];
                self.dataDic = responseObject[@"data"];
                
                NSString *status;
                if ([self.dataDic[@"status"] integerValue]==1) {
                    status = @"已取消";
                }else{
                    status = @"正常";
                }
                NSString *type;
                if ([self.dataDic[@"mealTimeCategory"] integerValue]==1) {
                    type = @"早餐";
                }else if ([self.dataDic[@"mealTimeCategory"] integerValue]==2) {
                    type = @"午餐";
                }else if ([self.dataDic[@"mealTimeCategory"] integerValue]==3) {
                    type = @"晚餐";
                }else if ([self.dataDic[@"mealTimeCategory"] integerValue]==4) {
                    type = @"其他";
                }
                NSArray *titleArr = @[[NSString stringWithFormat:@"订单号：%@",self.dataDic[@"orderId"]],
                                 [NSString stringWithFormat:@"订单时间：%@",self.dataDic[@"createTime"]],
                                 [NSString stringWithFormat:@"订单状态：%@",status],
                                 [NSString stringWithFormat:@"菜谱时间：%@",self.dataDic[@"recipeTime"]],
                                 [NSString stringWithFormat:@"用餐类型：%@",type]];
                for (int i = 0; i<self.labelArray.count; i++) {
                    UILabel *label = self.labelArray[i];
                    label.text = titleArr[i];
                }
                

                self.dataArray = [NSMutableArray array];
                for (int i = 0; i<arr.count; i++) {
                    NSDictionary *dic = arr[i];
                    MOFoodOrderListDetailModel *model = [[MOFoodOrderListDetailModel alloc]init];
                    model.canTingNameStr = responseObject[@"data"][@"restaurantName"];
                    model.foodNameStr = dic[@"dishName"];
                    model.numberStr = dic[@"servings"];
                    model.dishId = dic[@"dishId"];
                    [self.dataArray addObject:model];
                }
                [self.tableView reloadData];
            }
            
        } failure:^(NSError *error) {
            NSLog(@"订单详情：%@",[error localizedDescription]);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }else{
        self.dataArray = [NSMutableArray array];
        for (int i = 0; i<self.model.details.count; i++) {
            NSDictionary *dic = self.model.details[i];
            MOFoodOrderListDetailModel *model = [[MOFoodOrderListDetailModel alloc]init];
            model.canTingNameStr = self.model.restaurantName;
            model.foodNameStr = dic[@"dishName"];
            model.numberStr = dic[@"servings"];
            model.dishId = dic[@"dishId"];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
    }
    
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"Cell";
    MOFoodOrderListDetailViewCell *foodOrderListCell = [[MOFoodOrderListDetailViewCell alloc]cellCreat:cellIdentifier AndArray:self.buttonArray andTableView:tableView cellIndexPath:indexPath andType:0];
    foodOrderListCell.delegate = self;
    foodOrderListCell.model = self.dataArray[indexPath.section];
    return foodOrderListCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00000001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - YPSwipeTableViewCelldidSelectBtnWithTag 代理
- (void)YPSwipeTableViewCelldidSelectBtnWithTag:(NSInteger)tag andIndexPath:(NSIndexPath *)indexpath{
    
    MOFoodOrderListDetailModel *model = self.dataArray[indexpath.section];
    if (tag==0) {//评价
        MOAppraiseViewController *vc = [[MOAppraiseViewController alloc]init];
        vc.restaurantId = self.model.restaurantId;
        vc.dishId = model.dishId;
        vc.restaurantName = self.model.restaurantName;
        vc.dishName = model.foodNameStr;
        vc.type = self.model.mealTimeCategory;
        [self.navigationController pushViewController:vc animated:YES];
    }else{//详情
        [self getFoodDetailAndDishId:model.dishId];
    }
    
}

-(void)getFoodDetailAndDishId:(NSString *)dishId{
    
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",GETDISHBYID,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"dishId":dishId
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:postUrl parameters:dic success:^(id responseObject) {
        NSLog(@"菜品详情：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {

            MOFoodDetailView *view = [[MOFoodDetailView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            MOFoodDetailModel *detailModel = [[MOFoodDetailModel alloc]init];
            detailModel.foodNameStr = responseObject[@"data"][@"dishName"];
            detailModel.price = [NSString passNullForString:responseObject[@"data"][@"price"]];
            detailModel.cook = [NSString passNullForString:responseObject[@"data"][@"cook"]];
            detailModel.yuanLiao = [NSString passNullForString:responseObject[@"data"][@"material"]];
            detailModel.mark = [NSString passNullForString:responseObject[@"data"][@"mark"]];
            
            view.model = detailModel;
            [self.view addSubview:view];
            
        }else{
            [[ADShowMessage shareShowMessage]showTextOnly:@"获取失败，稍后重试" showInView:self.view];
        }
    } failure:^(NSError *error) {
        NSLog(@"菜品详情：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
    
}

-(void)clickBackButton
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark -懒加载
-(UIView *)navView
{
    if (!_navView) {
        _navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, StatusBarAndNavigationBarHeight)];
        _navView.backgroundColor = [UIColor whiteColor];
        
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 0, 25, 25)];
        [backButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
        [backButton setImage:IMAGE(@"nav_back") forState:UIControlStateNormal];
        backButton.centerY = StatusBarHeight + (StatusBarAndNavigationBarHeight - StatusBarHeight)/2;
        [_navView addSubview:backButton];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 32 - 100, 25)];
        titleLabel.text = @"订单详情";
        titleLabel.font =  [UIFont boldSystemFontOfSize:18];
        titleLabel.textColor = KBlackColor_222222;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.center = CGPointMake(SCREEN_WIDTH/2, backButton.centerY);
        [_navView addSubview:titleLabel];
    }
    return _navView;
}

-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
        _headerView.backgroundColor = KGrayColor_f8f8f8;
        
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, _headerView.width-20, 80)];
        backView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:backView];
        
        NSString *type;
        if ([self.model.mealTimeCategory integerValue]==1) {
            type = @"早餐";
        }else if ([self.model.mealTimeCategory integerValue]==2) {
            type = @"午餐";
        }else if ([self.model.mealTimeCategory integerValue]==3) {
            type = @"晚餐";
        }else if ([self.model.mealTimeCategory integerValue]==4) {
            type = @"其他";
        }
        NSString *status;
        if ([self.model.status integerValue]==1) {
            status = @"已取消";
        }else{
            status = @"正常";
        }
        self.labelArray = [NSMutableArray array];
        NSArray *arr = @[[NSString stringWithFormat:@"订单号：%@",_model.orderId],
                         [NSString stringWithFormat:@"订单时间：%@",_model.createTime],
                         [NSString stringWithFormat:@"订单状态：%@",status],
                         [NSString stringWithFormat:@"菜谱时间：%@",_model.recipeTime],
                         [NSString stringWithFormat:@"用餐类型：%@",type]];
        CGFloat mY = 0;
        for (int i = 0; i<arr.count; i++) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(19, 19*(i+1)+14*i, backView.width-38, 14)];
            label.font = FONT_SIZE(13);
            label.textColor = KBlackColor_2A2A2A;
            label.text = arr[i];
            [backView addSubview:label];
            mY = label.bottom;
            [self.labelArray  addObject:label];
        }
        
        backView.height = mY + 19;
        
        _headerView.height = backView.bottom;
        
    }
    return _headerView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = KGrayColor_f8f8f8;
    }
    return _tableView;
}


-(NSMutableArray *)buttonArray
{
    UIButton *btn11 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 80)];
    btn11.backgroundColor = [UIColor whiteColor];
    [btn11 setTitle:@"评价" forState:UIControlStateNormal];
    [btn11 setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
    [btn11 setImage:[UIImage imageNamed:@"dingcan_blackComent"] forState:UIControlStateNormal];
    btn11.titleLabel.font = FONT_SIZE(12);
    btn11.userInteractionEnabled = NO;
    [btn11 SG_imagePositionStyle:(SGImagePositionStyleTop) spacing:5];
    
    UIButton *btn22 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 80)];
    btn22.backgroundColor = [UIColor whiteColor];
    [btn22 setTitle:@"菜品详情" forState:UIControlStateNormal];
    [btn22 setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
    [btn22 setImage:[UIImage imageNamed:@"dingcan_blackDetail"] forState:UIControlStateNormal];
    btn22.titleLabel.font = FONT_SIZE(12);
    btn22.userInteractionEnabled = NO;
    [btn22 SG_imagePositionStyle:(SGImagePositionStyleTop) spacing:5];
    
    UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    btn2.backgroundColor = KGrayColor_f8f8f8;
    [btn2 addSubview:btn22];
    
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    btn1.backgroundColor = KGrayColor_f8f8f8;
    [btn1 addSubview:btn11];
    
    _buttonArray = [[NSMutableArray alloc]initWithObjects:btn1,btn2, nil];
    
    return _buttonArray;
}

@end
